﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YJKAreaForDraw
{
	public partial class DlgAreaSelToolbar : System.Windows.Forms.Form
	{
		public bool PickButton
		{
			get
			{
				return this.m_PickButton;
			}
			set
			{
				this.m_PickButton = value;
			}
		}

		public bool IsContinuous
		{
			get
			{
				return this.chbIsContinuous.Checked;
			}
		}

		public DlgAreaSelToolbar.OperateType CurrentOperateType
		{
			get
			{
				return this.m_CurrentOperateType;
			}
		}

		public bool IfOk
		{
			get
			{
				return this.m_bIfOk;
			}
		}

		public bool IfClose
		{
			get
			{
				return this.m_bIfClose;
			}
		}

		public bool IfWinFormClose
		{
			get
			{
				return this.m_bIfWinFormClose;
			}
			private set
			{
				this.m_bIfWinFormClose = value;
			}
		}

		public DlgAreaSelToolbar(bool bisClose)
		{
			this.InitializeComponent();
			base.ControlBox = bisClose;
		}

		private void EditBaseLineForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void EditBaseLineForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.m_bIfClose = true;
		}

		private void EditBaseLineForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				this.m_bIfWinFormClose = true;
				base.WndProc(ref msg);
				return;
			}
			base.WndProc(ref msg);
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void tsbDrawLine_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_DrawLine;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = true;
			this.ChangeOperateState();
		}

		private void tsbDrawArc3Pt_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_DrawArc3Pt;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = true;
			this.ChangeOperateState();
		}

		private void tsbDrawRect_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_DrawRect;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbDrawArcCenter_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_DrawArcCenter;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbSelectCurve_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_SelectRefCurve;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbSelectRef_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_SelectRef;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbDelete_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_Delete;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbFinish_Click(object sender, EventArgs e)
		{
			this.m_bIfOk = true;
			this.KeyEsc();
		}

		private void ReadData()
		{
		}

		private void SaveData()
		{
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void EditBaseLineForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (this.m_bIfOk || !this.m_bIfWinFormClose)
			{
				return;
			}
			this.SimulationPressESC();
		}

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgAreaSelToolbar.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgAreaSelToolbar.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			DlgAreaSelToolbar.SendMessage(SelectAreaManager._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void KeyEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgAreaSelToolbar.SendMessage(SelectAreaManager._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgAreaSelToolbar.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgAreaSelToolbar.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SetSelectType(DlgAreaSelToolbar.OperateType optType)
		{
			if (optType == DlgAreaSelToolbar.OperateType.OT_DrawLine)
			{
				this.tsbDrawLine.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawLine.CheckState = CheckState.Unchecked;
			}
			if (optType == DlgAreaSelToolbar.OperateType.OT_DrawRect)
			{
				this.tsbDrawRect.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawRect.CheckState = CheckState.Unchecked;
			}
			if (optType == DlgAreaSelToolbar.OperateType.OT_DrawArc3Pt)
			{
				this.tsbDrawArc3Pt.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			}
			if (optType == DlgAreaSelToolbar.OperateType.OT_DrawArcCenter)
			{
				this.tsbDrawArcCenter.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			}
			if (optType == DlgAreaSelToolbar.OperateType.OT_SelectRefCurve)
			{
				this.tsbSelectCurve.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbSelectCurve.CheckState = CheckState.Unchecked;
			}
			if (optType == DlgAreaSelToolbar.OperateType.OT_SelectRef)
			{
				this.tsbSelectRef.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbSelectRef.CheckState = CheckState.Unchecked;
			}
			if (optType == DlgAreaSelToolbar.OperateType.OT_Delete)
			{
				this.tsbDelete.CheckState = CheckState.Checked;
				return;
			}
			this.tsbDelete.CheckState = CheckState.Unchecked;
		}

		private void chbIsContinuous_CheckedChanged(object sender, EventArgs e)
		{
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawRect_MouseEnter(object sender, EventArgs e)
		{
		}

		private void tsbDrawArc3Pt_MouseEnter(object sender, EventArgs e)
		{
		}

		private void tsbDrawArcCenter_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbSelectCurve_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbSelectRef_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDelete_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawLine_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void toolStripEdit_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
		}

		private bool m_bIfOk;

		private bool m_bIfClose;

		private bool m_bIfWinFormClose;

		private DlgAreaSelToolbar.OperateType m_CurrentOperateType = DlgAreaSelToolbar.OperateType.OT_DrawLine;

		private bool m_PickButton;

		public enum OperateType
		{
			OT_DrawLine = 1,
			OT_DrawRect,
			OT_DrawArc3Pt,
			OT_DrawArcCenter,
			OT_SelectRefCurve,
			OT_SelectRef,
			OT_Delete
		}
	}
}
